package com.ideafactory.client.views;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ideafactory.client.beans.LeftGoodsBean;
import com.ideafactory.client.ybservice.APP;
import com.ideafactory.client.ybselfservice.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

/**
 * Created by LiuShao on 2016/5/24.
 */
public class LeftViewAdapter extends RecyclerView.Adapter<LeftViewAdapter.LeftViewHolder>{

    private LayoutInflater inflater;

    public LeftViewAdapter(){
        inflater = LayoutInflater.from(APP.getAPP().getApplicationContext());
    }

    private List<LeftGoodsBean.CatalogList> catalogLists;

    public void setCatalogLists(List<LeftGoodsBean.CatalogList> catalogLists){
        this.catalogLists =catalogLists;
    }

    private int currentPosition = 0;
    public  void setPosition (int position){
        this.currentPosition = position;
        notifyDataSetChanged();
    }

    public interface OnItemClickLinster {
        void onItemClick(View view, int position);
    }
    private OnItemClickLinster onItemClickLinster;
    public void setOnItemClickLinster(OnItemClickLinster onItemClickLinster) {
        this.onItemClickLinster = onItemClickLinster;
    }

    @Override
    public LeftViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.left_view_item, parent, false);
        LeftViewHolder viewHolder = new LeftViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(LeftViewHolder holder, int position) {
        holder.setIsRecyclable(false);
        holder.setData(position, holder.itemView);
    }

    @Override
    public int getItemCount() {
        return catalogLists == null ? 0 : catalogLists.size();
    }

    class LeftViewHolder extends RecyclerView.ViewHolder{

        @ViewInject(R.id.left_view_image)
        ImageView left_view_image;

        @ViewInject(R.id.left_view_text)
        TextView left_view_text;

        @ViewInject(R.id.ll_left_view)
        LinearLayout ll_left_view;

        public LeftViewHolder(View itemView) {
            super(itemView);
            x.view().inject(this, itemView);
        }

        public void setData(int position, final View itemView){

            if(position == currentPosition){
               ll_left_view.setBackgroundResource(R.mipmap.left_selected_back2);

            }else{
//                ll_left_view.setBackgroundColor(Color.TRANSPARENT);
                ll_left_view.setBackgroundResource(R.drawable.shape_leftview);
            }

            left_view_text.setText(catalogLists.get(position).catalogName);

            ImageLoader.getInstance().displayImage(catalogLists.get(position).icon, left_view_image, leftGoodsOption());

            if (onItemClickLinster != null) {
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = getLayoutPosition();
                        onItemClickLinster.onItemClick(itemView, pos);
                    }
                });
            }

        }

    }

    DisplayImageOptions leftGoodsOp;
    public DisplayImageOptions leftGoodsOption(){
        if(leftGoodsOp==null){
            leftGoodsOp = new DisplayImageOptions.Builder()
                    .showImageOnFail(R.mipmap.image_load_failed)  //设置图片加载/解码过程中错误时候显示的图片
                    .cacheInMemory(true)//设置下载的图片是否缓存在内存中
                    .cacheOnDisc(true)//设置下载的图片是否缓存在SD卡中
                    .bitmapConfig(Bitmap.Config.RGB_565)
                    .displayer(new SimpleBitmapDisplayer())
                    .build();//构建完成
        }
        return leftGoodsOp;
    }

}
